﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Knight.Common.Helper
{
    public class IOHelper
    {
        #region XML
        /// <summary>
        /// XML序列化
        /// </summary>
        public static bool SerializeToXml(object obj, string filePath)
        {
            bool result = false;

            FileStream fs = null;
            try
            {
                fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(fs, obj);
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
            return result;

        }
        /// <summary>
        /// XML反序列化
        /// </summary>
        public static object DeserializeFromXML(Type type, string filePath)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(type);
                return serializer.Deserialize(fs);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
        }
        #endregion

        #region JSON
        /// <summary>
        /// Json序列化
        /// </summary>
        public static bool SerializeToJson(object obj, string filePath)
        {
            bool result = false;

            StreamWriter sw = null;
            try
            {
                sw = new StreamWriter(filePath);
                JsonSerializer serializer = new JsonSerializer();
                serializer.Converters.Add(new JavaScriptDateTimeConverter());
                serializer.NullValueHandling = NullValueHandling.Ignore;

                JsonWriter writer = new JsonTextWriter(sw);
                serializer.Serialize(writer, obj);
                writer.Close();
                sw.Close();
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (sw != null)
                    sw.Close();
            }
            return result;

        }
        /// <summary>
        /// Json反序列化
        /// </summary>
        public static object DeserializeFromJson(Type type, string filePath)
        {
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(filePath);
                JsonSerializer serializer = new JsonSerializer();

                return serializer.Deserialize(sr, type);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (sr != null)
                    sr.Close();
            }
        }

        /// <summary>
        /// Json反序列化
        /// </summary>
        public static object DeserializeFromJson(Type type, Stream stream)
        {
            try
            {
                StreamReader sr = new StreamReader(stream);
                JsonSerializer serializer = new JsonSerializer();
                return serializer.Deserialize(sr, type);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Json反序列化
        /// </summary>
        public static T DeserializeFromJson<T>(Stream stream) where T : class
        {
            return DeserializeFromJson(typeof(T), stream) as T;
        }
        #endregion
    }
}
